<template lang="pug">
  div
    Form(:model='formData', :label-width=60)
      FormItem(label='标题')
        Input(v-model='formData.title')
      FormItem(label='内容')
        Editor(v-model='formData.body')
      //- FormItem(label='发布')
        i-switch(v-model='formData.published')
          Icon(type='android-done', slot='open')
          Icon(type='android-close', slot='close')
      //- FormItem(label='加入菜单')
        i-switch(v-model='formData.asMenuItem')
          Icon(type='android-done', slot='open')
          Icon(type='android-close', slot='close')
      FormItem
        Button(type='primary', @click='submit', :loading='loading') {{ loading ? '保存中...' : '保存' }}
</template>

<script>
import Editor from '@/components/UEditor'
import Api from '@/apis/page'

export default {
  components: {
    Editor
  },
  beforeRouteEnter (to, from, next) {
    Api.getPageDetail(to.params.id).then(page => {
      next(vm => {
        vm.page = page
        vm.formData.title = page.title
        vm.formData.body = page.body
        // vm.formData.published = page.published
        vm.formData.asMenuItem = page.asMenuItem
      })
    })
  },
  data () {
    return {
      loading: false,
      formData: {
        id: this.$route.params.id,
        gid: this.$store.state.app.id,
        title: '',
        body: '',
        // published: false,
        asMenuItem: false
      }
    }
  },
  methods: {
    submit () {
      this.loading = true
      Api.patchPage(this.formData).then(() => {
        this.loading = false
        this.$Message.success('保存成功')
        this.$router.push('/pages')
      })
    }
  }
}
</script>
